﻿@{
    ViewData["Title"] = "系统登录";
}
@{ 
    Layout = null;
}

<link rel="stylesheet" type="text/css" href="/css/login.css" />
<style>
    .layui-layer-hui .layui-layer-content{
        color:white;
    }
</style>
<script src="/lib/layui/layui.all.js"></script>
<title>信息中心IT资产管理系统</title>
<div class="bg">
    <div class="wel">IT项目管理系统</div>
    <div class="user">
        <div id="yonghu" style="">用户名</div>
        <input type="text" name="用户" value="" id="name" />
    </div>
    <div class="password">
        <div id="yonghu">密&nbsp;&nbsp;&nbsp;码</div>
        <input class="" type="password" name="密码" value="" id="password" />
    </div>
    
    <div class="rem">
        <input type="checkbox" name="remember" id="rememberPass" />
        <div id="reb">
            记住密码
        </div>
    </div>

    <!--
     <div class="fg">
         <div style="font-size: 11px;margin-top: 11px;">
             <a style="font-size: 11px;" href="#">忘记密码？</a>
         </div>
     </div>
        -->
    <input class="btn" type="button" name="登录" value="登录" onclick="doLogin()" />
</div>
<script type="text/javascript">

    if(window.top != window) {
        window.top.location.reload();
    }

    var $ = layui.$
        , layer = layui.layer;

    $('#name').focus();
    $(document).on('keydown', function (e) {  
        if (e.keyCode == 13){
            doLogin();
        } 
    })

    $(document).ready(function () {
        var name = getCookie("username");
        var pass = getCookie("pass");
        $("#name").val(name);
        $("#password").val(pass);

        if (name.length > 2 && pass.length > 2) {
            $('input[name="remember"]').attr("checked", "checked");
            layui.form.render();
        } 
    });

    function doLogin() {
        var name = $('#name').val();
        var pass = $('#password').val(); 

        if (name.length == 0) {
            layer.msg("请输入用户名");
            return;
        }

        if (pass.length == 0) {
            layer.msg("请输入密码");
            return;
        }


        $.ajax({
            url: "/login/doLogin/",
            type: "post",
            data: {
                name: name,
                password: pass,
                rememberPass: $('#rememberPass').is(":checked")
            },
            success: function (res) {
                layer.msg(res.msg);
                if (res.code == 1) {

                    delCookie("username");
                    delCookie("pass");
                    var isChecked = $('input[name="remember"]:checked').length > 0;
                    if (isChecked) {
                        setCookie("username", name, 365);
                        setCookie("pass", pass, 365);
                    } else {
                        setCookie("username", name, -365);
                        setCookie("pass", pass, -365);
                    }

                    window.location.href = "/home/index";
                }
            }
        });
    }

    function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toGMTString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }

    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i].trim();
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    }

    function delCookie(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = getCookie(name);
        if (cval != null)
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
</script>